package com.code.modules.base.controller;

import com.code.common.utils.MD5Util;
import com.code.common.utils.PageUtil;
import com.code.common.utils.ToolUtil;
import com.code.common.vo.ResultData;
import com.code.config.Swagger.params.ApiJsonObject;
import com.code.config.Swagger.params.ApiJsonProperty;
import com.code.modules.base.model.User;
import com.code.modules.base.service.UserService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(value = "用户控制器", tags = {"用户接口"})
public class UserController {


    @Autowired
    private UserService userService;


    @RequiresAuthentication
    @ApiOperation("根据id查询用户信息")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/findById")
    public ResultData findUserById(@ApiJsonObject(name = "User_findUserById_Map", value = {
            @ApiJsonProperty(key = "id",  example = "0",  description = "用户id", type = int.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer id = (Integer) params.get("id");
        User user = userService.selectById(id);
        if(user != null) {
            return new ResultData().succ(200,"查询成功", user);
        }
        return new ResultData().fail(400,"查询失败");
    }

    @RequiresAuthentication
    @ApiOperation("根据帐号查询用户")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/findByUsername")
    public ResultData findUserByUsername(@ApiJsonObject(name = "User_findUserByUsername_Map", value = {
            @ApiJsonProperty(key = "username",  example = "username",  description = "用户账号", type = String.class, required = true) })
            @RequestBody Map<String,Object> params) {
        String username = (String)params.get("username");
        User user  = userService.findUserByusername(username);
        if(user!=null){
            return new ResultData().succ(200,"查询成功",user);
        }else{
            return new ResultData().fail(400,"查询失败");
        }
    }

    @RequiresAuthentication
    @ApiOperation("修改头像")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/avatar")
    public ResultData updateAatar(@ApiJsonObject(name = "User_updateAatar_Map", value = {
            @ApiJsonProperty(key = "uid",  example = "0",  description = "用户id", type = int.class, required = true),
            @ApiJsonProperty(key = "avatarurl",  example = "头像地址",  description = "头像地址", type = String.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer uid = (Integer)params.get("uid");
        String avatarurl = (String)params.get("avatarurl");
        boolean flag = userService.setUserAatar(uid,avatarurl);
        if(flag == true) {
            return new ResultData().succ(200,"修改头像成功");
        }
        return new ResultData().fail(400,"修改头像失败");
    }

    @RequiresAuthentication
    @RequiresPermissions("user:pageList")
    @ApiOperation("用户分页")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/pageList")
    public ResultData pageList(@ApiJsonObject(name = "User_pageList_Map", value = {
            @ApiJsonProperty(key = "currentPage",  example = "1",  description = "当前页", type = int.class, required = true),
            @ApiJsonProperty(key = "size",  example = "10",  description = "每页数量", type = int.class, required = true),
            @ApiJsonProperty(key = "key",  example = "查询内容",  description = "查询内容", type = String.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer currentPage = (Integer) params.get("currentPage");
        Integer size = (Integer) params.get("size");
        String key = (String) params.get("key");
        List<User> ulist = userService.getPageList(key);
        if(ulist!=null){
            PageUtil<User> upage = new PageUtil<User>();
            upage.queryPager(currentPage,size,ulist);
            return new ResultData().succ(200,"查询成功",upage);
        }
        return new ResultData().fail(400,"查询失败");
    }

    @RequiresAuthentication
    @RequiresPermissions("user:add")
    @ApiOperation("添加用户")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/add")
    public ResultData addUser(@RequestBody User user) {
        if(ToolUtil.isNotEmpty(user)) {
            User u  = userService.findUserByusername(user.getUsername());
            if(u == null) {
                user.setSalt(MD5Util.getRandomSalt(5));
                user.setPassword(MD5Util.md5(user.getPassword(),user.getSalt()));
                user.setCreatetime(new Date());
                user.setUpdatetime(new Date());
                userService.insert(user);
                return new ResultData().succ(200,"添加成功");
            } else {
                return new ResultData().fail(400,"用户账号已占用");
            }
        }
        return new ResultData().fail(400,"添加失败");
    }

    @RequiresAuthentication
    @RequiresPermissions("user:edit")
    @ApiOperation("修改用户")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/edit")
    public ResultData editUser(@RequestBody User user) {
        if(ToolUtil.isNotEmpty(user)) {
            user.setPassword(null);
            user.setUpdatetime(new Date());
            userService.updateById(user);
            return new ResultData().succ(200,"修改用户成功");
        }
        return new ResultData().fail(400,"修改用户失败");
    }

    @RequiresAuthentication
    @RequiresPermissions("user:del")
    @ApiOperation("删除用户")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/del")
    public ResultData delUser(@ApiJsonObject(name = "User_delUser_Map", value = {
            @ApiJsonProperty(key = "userid",  example = "1",  description = "用户Id", type = int.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer userid =  (Integer) params.get("userid");
        boolean flag = userService.deleteById(userid);
        if(flag == true) {
            return new ResultData().succ(200,"删除成功");
        }
        return new ResultData().fail(400,"删除失败");

    }

    @RequiresAuthentication
    @RequiresPermissions("user:updatepwd")
    @ApiOperation("修改密码")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/updatepwd")
    public ResultData updatePWD(
            @ApiJsonObject(name = "User_updatePWD_Map", value = {
                    @ApiJsonProperty(key = "uid",  example = "1",  description = "用户ID", type = int.class, required = true),
                    @ApiJsonProperty(key = "oldPassword", example = "123456", description = "原密码", type = String.class, required = false),
                    @ApiJsonProperty(key = "newPassword", example = "666888", description = "新密码", type = String.class, required = false),
                    @ApiJsonProperty(key = "confirmNewPassword", example = "666888", description = "确认密码", type = String.class, required = false) })
            @RequestBody Map<String,Object> params) {
        Integer uid = (Integer) params.get("uid");
        String oldPassword = (String)params.get("oldPassword");
        String newPassword = (String)params.get("newPassword");
        String confirmNewPassword = (String)params.get("confirmNewPassword");
        if(!newPassword.equals(confirmNewPassword)) {
            return new ResultData().fail(400,"输入两次新密码不一致");
        }
        User u = userService.selectById(uid);
        String oldMd5 = MD5Util.md5(oldPassword,u.getSalt());
        if(oldMd5.equals(u.getPassword())) {
            String newMd5 = MD5Util.md5(confirmNewPassword,u.getSalt());
            userService.changeUserPwd(u.getId(),newMd5,u.getSalt());
            return new ResultData().succ(200,"修改密码成功");
        }
        return new ResultData().fail(400,"原密码错误，修改失败");
    }


    @RequiresAuthentication
    @RequiresPermissions("user:reset")
    @ApiOperation("重置密码666888")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/reset")
    public ResultData resetPWD(@ApiJsonObject(name = "User_resetPWD_Map", value = {
            @ApiJsonProperty(key = "uid",  example = "1",  description = "用户Id", type = int.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer uid = (Integer) params.get("uid");
        String salt = MD5Util.getRandomSalt(5);
        User u = userService.selectById(uid);
        u.setSalt(salt);
        String newMd5 = MD5Util.md5("666888",u.getSalt());
        userService.changeUserPwd(u.getId(),newMd5,u.getSalt());
        return new ResultData().succ(200,u.getUsername()+"重置密码666888");
    }


    @RequiresAuthentication
    @RequiresPermissions("user:setrole")
    @ApiOperation("分配角色")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/setrole")
    public ResultData setRole(@ApiJsonObject(name = "User_setRole_Map", value = {
            @ApiJsonProperty(key = "userid",  example = "1",  description = "用户ID", type = int.class, required = true),
            @ApiJsonProperty(key = "roleids", example = "[1,2,3]", description = "角色组", type = String.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer userid = (Integer)params.get("userid");
        List<Integer> roleids = (List<Integer>)params.get("roleids");
        userService.setUserRoles(userid, roleids);
        return new ResultData().succ(200,"分配角色成功");
    }




















}

